import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 线程安全demo CAS解决线程问题
 *
 * @author brianxia
 * @version 1.0
 * @date 2021/12/9 16:37
 */
public class ThreadSafeDemo4 {

    public static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {


        //1.创建两个线程
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 1000000; i++) {
                count.incrementAndGet();
            }

        });

        //2.分别对count ++
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 1000000; i++) {
                count.incrementAndGet();
            }

        });


        thread.start();
        thread2.start();
        //3.打印结果
        thread.join();
        thread2.join();
        System.out.println(count);

        //ABA问题

        AtomicStampedReference<Integer> integerAtomicStampedReference =
                new AtomicStampedReference<Integer>(0,1);

//        integerAtomicStampedReference.compareAndSet()

    }

}
